//
//  HttpRouter.swift
//  CitySearch
//
//  Created by guotonglin on 2020/6/2.
//  Copyright © 2020 guotonglin. All rights reserved.
//

import Foundation

import Alamofire

protocol HttpRouter: URLRequestConvertible {
    var baseUrlString: String { get }
    var path: String { get }
    var method: HTTPMethod { get }
    var header: HTTPHeaders? { get }
    var parameters: Parameters? { get }
    func body() throws -> Data?
    func request(usingService  service: HttpService)throws -> DataRequest
}


extension HttpRouter {
    
    var parameters: Parameters? { return nil }
    
    func body() throws -> Data? { return nil }
    
    func asURLRequest() throws -> URLRequest {
        var url = try baseUrlString.asURL()
        url.appendPathComponent(path)
        var request = try URLRequest(url: url, method: method, headers: header)
        request.httpBody = try body()
        return request
    }
}

extension HttpRouter {
    func request(usingService service: HttpService) throws -> DataRequest {
        return service.request(self)
    }
}
